home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT1-5 / NUMIO.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-08-27  |  2.9 KB  |  55 lines

  1. ;
  2. ;       Program NumIO ( Chapter 3 )
  3. ;
  4. .model  small                   ; small memory model - one segment only
  5. .stack                          ; stack of default size
  6. .data                           ; this starts data segment
  7. CR      equ     0Dh             ; Carriage Return
  8. LF      equ     0Ah             ; Line Feed
  9. EndMsg  equ     '$'             ; End of message
  10. MsgIn   db      CR,LF,'Enter a number less than 65537: ',EndMsg
  11. MsgOut  db      CR,LF,'You have entered the number ',EndMsg
  12. UnsWord dw      0               ; number to be output
  13. Ten     dw      10              ; constant for obtaining decimal digits
  14. .code                           ; this starts code segments
  15. .startup                        ; this initializes segment registers
  16.     mov     ah,09h          ; function 09h - output text string
  17.     lea     dx,MsgIn        ; address of message into DX
  18.     int     21h             ; DOS service call
  19. NextCh: mov     ah,01           ; function 01h - keyboard input
  20.     int     21h             ; DOS service call
  21.     cmp     al,0            ; special character?
  22.     jne     NotSpec         ; if not - process character
  23.     int     21h             ; read code of special character
  24.     jmp     short OutNum          ; output number
  25. NotSpec:cmp     al,'0'          ; compare character read to "0" (number?)
  26.     jb      OutNum          ; if not, don't process
  27.     cmp     al,'9'          ; compare character read to "9" (number?)
  28.     ja      OutNum          ; if not, don't process 
  29. ProcNum:sub     al,'0'          ; convert character to number
  30.     mov     bl,al           ; copy character read into BX
  31.     mov     ax,UnsWord      ; hex number into AX
  32.     mul     Ten             ; one decimal position to the left
  33.     mov     UnsWord,ax      ; store result back into memory
  34.     add     UnsWord,bx      ; add current hex digit
  35.     jmp     short NextCh          ; read next character
  36. OutNum: mov     ah,09h          ; function 09h - output text string
  37.     lea     dx,MsgOut       ; address of message into DX
  38.     int     21h             ; DOS service call
  39.     mov     ax,UnsWord      ; place number to be printed into AX.
  40.     mov     cx,0            ; clear counter of digits 
  41. NexDiv: mov     dx,0            ; clear high part of number
  42.     div     Ten             ; divide number to be prinred by 10
  43.     push    dx              ; push remainder (current digit) into stack.
  44.     inc     cx              ; increase counter of digits
  45.     cmp     ax,0            ; result is zero? (number was less than 10)
  46.     jne     NexDiv          ; if not, continue process (get next digit)
  47.     mov     ax,0200h        ; function 02h - output character
  48. OutSym: pop     dx              ; pop current digit from stack
  49.     add     dl,'0'          ; convert digit to character
  50.     int     21h             ; DOS service call
  51.     loop    OutSym          ; to output next digit
  52. FinProg:mov     ax,4C00h        ; function 4Ch - terminate process (exit code 0)
  53.     int     21h             ; DOS service call
  54.     end     
  55.